package ch.zhaw.swp1.model.message;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import ch.zhaw.swp1.model.message.address.Address;
import ch.zhaw.swp1.model.message.address.EmailAddress;
import ch.zhaw.swp1.model.message.body.EmailBody;
import ch.zhaw.swp1.model.status.Status;
import ch.zhaw.swp1.model.status.error.MessageAttachmentError;
import ch.zhaw.swp1.util.MultiChannelParser;

/**
 * Class representing an Email-Message
 * @author Daniel Tiefenauer
 */
public class Email extends Message {

	/**
	 * Default version
	 */
	private static final long serialVersionUID = 1L;
	private Vector<File> attachments = new Vector<File>();

	/************************************************************************************/
	/* Constructors
	/*----------------------------------------------------------------------------------*/
	/**
	 * Default constructor
	 */
	public Email() {
		setSender(new EmailAddress());
		setBody(new EmailBody());
	}
	
	/**
	 * Overloaded constructor with parameters
	 * @param recipientLine user entered recipient line
	 * @param sender user entered sender address
	 * @param body user entered message text
	 */
	public Email(String recipientLine, String sender, String body){
		// instantiate with correct types		
		addRecipientList(MultiChannelParser.parseRecipients(recipientLine, MessageType.EMAIL));
		setSender(new EmailAddress(sender));
		setBody(new EmailBody(body));
	}
	
	/**
	 * Overloaded constructor with parameters
	 * @param recipientList a list of recipients (already parsed)
	 * @param sender user entered sender address
	 * @param body user entered message text
	 */
	public Email(List<Address> recipientList, String sender, String body){
		addRecipientList(recipientList);
		setSender(new EmailAddress(sender));
		setBody(new EmailBody(body));
	}
	
	/************************************************************************************/
	/* Methods
	/*----------------------------------------------------------------------------------*/
	/**
	 * Add an attachment to the list
	 * @param file a File-object representing a file on the harddisk
	 */
	public void addAttachment(File file){
		attachments.add(file);
	}
	
	/**
	 * Send E-Mail message. Added functionality for attachments.
	 */
	public void send(){
		super.send();
		System.out.println("Attachments: ");
		Iterator<File> it = attachments.iterator();
		while(it.hasNext()){
			System.out.println("\t - " + it.next().getName());
		}
	}

	@Override
	public void addRecipient(String recipient) {
		addRecipient(new EmailAddress(recipient));		
	}

	/************************************************************************************/
	/* START Validation
	/*----------------------------------------------------------------------------------*/
	@Override
	public List<Status> validate() {
		super.validate();
		validateAttachments();
		return statusList;
	}
	
	/**
	 * Validate list of attachments
	 */
	private void validateAttachments(){
		// Iteration over attachments
		Iterator<File> it = attachments.iterator();
		while(it.hasNext()){
			File file = it.next();
			if(!file.exists()){
				statusList.add(new MessageAttachmentError(file));
			}
		}
	}
	/*----------------------------------------------------------------------------------*/
	/* END Validation
	/************************************************************************************/
	
	/************************************************************************************/
	/* START Setters/Getters
	/*----------------------------------------------------------------------------------*/
	@Override
	public void setSender(String sender) {
		setSender(new EmailAddress(sender));	
	}
	public void setBody(String text){
		setBody(new EmailBody(text));
	}
	public MessageType getMessageType(){
		return MessageType.EMAIL;
	}
	public List<File> getAttachments(){
		return attachments;
	}
	public void setAttachments(List<File> attachments){
		Iterator<File> it = attachments.iterator();
		while(it.hasNext()){
			this.attachments.add(it.next());
		}
	}
	/*----------------------------------------------------------------------------------*/
	/* END Setters/Getters
	/************************************************************************************/


}
